package com.neusoft.SPNursingHome.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.Food;
import com.neusoft.SPNursingHome.po.ResponseBean;
import com.neusoft.SPNursingHome.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService service;

    @GetMapping("/list")
    public ResponseBean<List<Food>> listAll(@RequestParam(required = false) String foodType) {
        return service.listAll(foodType);
    }

    @GetMapping("/list-change")
    public ResponseBean<IPage<Food>> listChange(
            @RequestParam(required = false) String foodType,
            @RequestParam(required = false) String foodName,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long size
    ) {
        IPage<Food> result = service.listChange(foodType, foodName, page, size);
        return new ResponseBean<>(result);
    }

    @GetMapping("/{id}")
    public ResponseBean<Food> getById(@PathVariable Integer id) {
        return service.getById(id);
    }

    @PostMapping("/create")
    public ResponseBean<Void> create(@RequestBody Food food) {
        return service.create(food);
    }

    @PutMapping("/update")
    public ResponseBean<Void> update(@RequestBody Food food) {
        return service.update(food);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean<Void> delete(@PathVariable Integer id) {
        return service.deleteById(id);
    }
}
